import tkinter as tk
from tkinter import ttk, messagebox
import mysql.connector


class SupplierManager:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("供应商管理")
        self.selected_supplier = None

        self.init_widgets()
        self.refresh_list()

    def init_widgets(self):
        # 列表框
        self.listbox = tk.Listbox(self.root, height=10)
        self.listbox.grid(row=0, column=0, columnspan=2, sticky='nsew', padx=10, pady=10)

        # 输入框（只列出客户编号作为示例，其他类似）
        tk.Label(self.root, text="客户编号:").grid(row=1, column=0, sticky='e')
        self.cid_entry = tk.Entry(self.root)
        self.cid_entry.grid(row=1, column=1, sticky='ew', padx=5)

        # ... 其他输入框类似，这里省略 ...

        # 按钮
        btn_frame = tk.Frame(self.root)
        btn_frame.grid(row=2, column=0, columnspan=2, sticky='ew', padx=10, pady=5)

        ttk.Button(btn_frame, text="增加", command=self.add_supplier).grid(row=0, column=0, padx=5, pady=5)
        ttk.Button(btn_frame, text="修改", command=self.edit_supplier).grid(row=0, column=1, padx=5, pady=5)
        ttk.Button(btn_frame, text="查询", command=self.search_supplier).grid(row=0, column=2, padx=5,
                                                                              pady=5)  # 简化为刷新列表
        ttk.Button(btn_frame, text="删除", command=self.delete_supplier).grid(row=0, column=3, padx=5, pady=5)
        ttk.Button(btn_frame, text="清空内容", command=self.clear_entries).grid(row=0, column=4, padx=5, pady=5)
        ttk.Button(btn_frame, text="确定", command=self.confirm_supplier).grid(row=0, column=5, padx=5, pady=5)

        # 绑定双击事件
        self.listbox.bind('<Double-1>', self.on_listbox_double_click)

    def refresh_list(self):
        # 清除列表框内容并重新填充
        self.listbox.delete(0, tk.END)
        self.connect_to_db()
        cursor = self.cnx.cursor()
        query = "SELECT * FROM tb_supplier"
        cursor.execute(query)
        for row in cursor:
            self.listbox.insert(tk.END, f"编号: {row[0]}, 名称: {row[1]}")
        cursor.close()

    def connect_to_db(self):
        self.cnx = mysql.connector.connect(
            user='root',
            password='AAA666111',
            host='localhost',
            database='test4'
        )

    def add_supplier(self):
        # 获取输入框中的数据
        cid = self.cid_entry.get().strip()
        cname = self.cname_entry.get().strip()  # 假设这是客户名称的输入框
        ccompanyname = self.ccompanyname_entry.get().strip()  # 假设这是客户小名的输入框
        ccompanyadress = self.ccompanyadress_entry.get().strip()
        ccompanyphone = self.ccompanyphone_entry.get().strip()
        ctellphone = self.ctellphone_entry.get().strip()
        cemail = self.cemail_entry.get().strip()

        # 检查客户编号是否为空
        if not cid:
            messagebox.showerror("错误", "客户编号不能为空！")
            return

            # 连接到数据库
        self.connect_to_db()
        cursor = self.cnx.cursor()

        # 插入数据到数据库
        query = """  
        INSERT INTO tb_supplier (Cid, Cname, Ccompanyname, CcompanyAdress, CcompanyPhone, CtellPhone, Cemail)  
        VALUES (%s, %s, %s, %s, %s, %s, %s)  
        """
        data = (cid, cname, ccompanyname, ccompanyadress, ccompanyphone, ctellphone, cemail)
        try:
            cursor.execute(query, data)
            self.cnx.commit()
            messagebox.showinfo("成功", "供应商添加成功！")
            self.refresh_list()  # 刷新列表框
        except mysql.connector.Error as e:
            messagebox.showerror("错误", f"添加供应商时出错: {e}")
        finally:
            cursor.close()

    def edit_supplier(self):
        # 如果选中了供应商，则编辑
        if self.selected_supplier:
            # 实现编辑逻辑
            pass

    def delete_supplier(self):
        # 如果选中了供应商，则删除
        if self.selected_supplier:
            # 实现删除逻辑
            pass

    def clear_entries(self):
        # 清空输入框内容
        for widget in self.root.winfo_children():
            if isinstance(widget, tk.Entry):
                widget.delete(0, tk.END)  # 删除Entry中的所有内容

    # ... 其他部分的代码保持不变 ...